package com.h3c.nem.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.UUID;

/**
 * @author wys43593
 * @date 2022/03/11
 *
 * topic消息队列配置
 */
@Configuration
public class TopicRabbitConfig {
    //绑定键
    @Value("${oper.log.code}")
    private String operLogQueue;
    @Value("${file.oper.log.code}")
    private String fileOperLogQueue;
    @Value("${auth.change.log.code}")
    private String authChgLogQueue;
    @Value("${ptlog.exchange.code}")
    private String exchange;

    public String getOperLogQueue() {
        return operLogQueue;
    }

    public String getFileOperLogQueue() {
        return fileOperLogQueue;
    }

    public String getAuthChgLogQueue() {
        return authChgLogQueue;
    }

    public String getExchange() {
        return exchange;
    }

    public Message getMessage(String logObj) {
        return MessageBuilder.withBody(logObj.getBytes())
                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                .setContentEncoding("utf-8")
                .setMessageId(UUID.randomUUID()+"")
                .build();
    }

    @Bean
    public Queue operLogQueue() {
        return new Queue(operLogQueue);
    }

    @Bean
    public Queue fileOperLogQueue() {
        return new Queue(fileOperLogQueue);
    }

    @Bean
    public Queue authChgLogQueue() {
        return new Queue(authChgLogQueue);
    }

    @Bean
    Binding operLogExchangeMessage() {
        //with(String routingKey)
        // routingKey: 配置队列的key，直接全匹配
        return BindingBuilder.bind(operLogQueue()).to(exchange()).with(operLogQueue);
    }

    @Bean
    Binding fileOperLogExchangeMessage() {
        return BindingBuilder.bind(fileOperLogQueue()).to(exchange()).with(fileOperLogQueue);
    }

    @Bean
    Binding authChgLogExchangeMessage() {
        return BindingBuilder.bind(authChgLogQueue()).to(exchange()).with(authChgLogQueue);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange(exchange);
    }

}
